package com.gym.controller.form.customer;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import com.gym.model.dao.ConnessioneException;
import com.gym.model.dao.DAOUser;
import com.gym.model.dao.DatoNonTrovatoException;
import com.gym.model.dao.MetodoDaoException;

public class FormRecuperaPassword extends ActionForm {

	private String email;

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Override
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

		ActionErrors errors = new ActionErrors();
		DAOUser dao;
		try {
			dao = new DAOUser();
			dao.selectByEmail(getEmail()).getEmail();
		
		} 
		catch (ConnessioneException | MetodoDaoException | DatoNonTrovatoException e) {
			
			e.printStackTrace();
			if( ! email.matches("[-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+.[a-zA-Z]{2,4}"))
				errors.add("email", new ActionMessage("it.errore.email.formato"));
			else
			errors.add("email", new ActionMessage("it.errore.elimina.verifica"));//msg not found
		}
		
		
		if (getEmail()==null || getEmail().isEmpty()) {
			errors.add("email", new ActionMessage("it.errore.email.obbligatiorio"));
		} 
		
		
		return errors;
	}
	
}
